"""
Test that image is not changed when:
1. Read in as VTK from disk
2. Converted from VTK -> ITK
3. Converted from ITK -> VTK
4. Saved to disk in VTK format  
"""

import connect2
import vtk
from time import time
import ImageCompare

inputFilename='C:/Lib/JointTrack/branches/bryan/Images/itktestimage.png'
outputFilename = 'C:/Lib/JointTrack/branches/bryan/Images/itkvtkoutput.png'

# VTK will read the PNG image for us
reader = vtk.vtkPNGReader()
reader.SetFileName(inputFilename)
reader.Update()

vtk2itk = connect2.vtk2itk()
itk2vtk = connect2.itk2vtk()

# convert from vtk to itk, and from itk to vtk
start = time()
vtk2itk.SetInput(reader.GetOutput())
itk2vtk.SetInput(vtk2itk.GetOutput())
out = itk2vtk.GetOutput()
out.Update()
stop = time()
TotalTime = stop-start

# finally write the image to disk using VTK (to confirm that it works)
writer = vtk.vtkPNGWriter()
writer.SetFileName(outputFilename)
writer.SetInput(itk2vtk.GetOutput())

# write the file to disk...
writer.Write()
comp = ImageCompare.compare(inputFilename,outputFilename)

#Test for positive match
assert comp==0

if comp==0:
    print "Finished!  VTK->ITK->VTK Time: ", TotalTime
else:
    print "Test Failue!"
    